package com.hspedu.tankgame5;

import java.io.*;
import java.util.Vector;

/**
 * @author DL5O
 * @version 1.0
 */
public class Recorder {
    private static int allEnemyTankNum = 0;
    private static BufferedWriter bw= null;
    private static BufferedReader br= null;
    private static Vector<EnemyTank> enemyTanks = new Vector<>();
    private static Vector<Node> nodes = new Vector<>();//定义一个vector 用于读取敌人坦克的信息
    private static String recordFile = "src\\myRecord.txt";


    //该方法，在继续上局游戏的时候调用即可
    //用于恢复读取recordFile，恢复相关信息
    public static Vector<Node> getNodesAndEnemyTankRec(){
        try {
            br = new BufferedReader(new FileReader(recordFile));
            allEnemyTankNum = Integer.parseInt(br.readLine());//当调用getNodes时可以永久保存击败敌人的数量
            String enemyInfo = "";
            while ((enemyInfo = br.readLine()) != null){
                String[] s = enemyInfo.split(" ");//用空格进行字符串的分隔，x y direct
                int x = Integer.parseInt(s[0]);
                int y = Integer.parseInt(s[1]);
                int direct = Integer.parseInt(s[2]);
                Node node = new Node(x, y, direct);
                //System.out.println("x=" + x + " y=" + y + " direct=" + direct);
                //将node存入到vector集合中
                nodes.add(node);
            }
            return nodes;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 用于往文件中存入信息，其中包括玩家击败的坦克数量，坦克的坐标，坦克的方向
     */
    public static void keepRecord(){
        try {
            bw = new BufferedWriter(new FileWriter(recordFile));
            bw.write(allEnemyTankNum + "");
            bw.newLine();
            //取出敌人的坦克，然后写入坐标(x,y) 和 索引号
            for (int i = 0; i < enemyTanks.size(); i++) {
                EnemyTank enemyTank = enemyTanks.get(i);
                if(enemyTank.isLive){
                    bw.write(enemyTank.getX() + " " +
                            enemyTank.getY() + " " + enemyTank.getDirect() + "\r\n");
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static int getAllEnemyTankNum() {
        return allEnemyTankNum;
    }

    public static void setAllEnemyTankNum(int allEnemyTankNum) {
        Recorder.allEnemyTankNum = allEnemyTankNum;
    }

    public static Vector<EnemyTank> getEnemyTanks() {
        return enemyTanks;
    }

    public static void setEnemyTanks(Vector<EnemyTank> enemyTanks) {
        Recorder.enemyTanks = enemyTanks;
    }

    public static String getRecordFile() {
        return recordFile;
    }

    public static void setRecordFile(String recordFile) {
        Recorder.recordFile = recordFile;
    }
}
